package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.GoodSMapper;
import com.itheima.goods.pojo.Goods;
import com.itheima.goods.service.GoodsService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Service;



@Service
public class GoodsServiceImpl extends ServiceImpl<GoodSMapper,Goods> implements GoodsService{
    @Override
    public Result findAll() {
        QueryWrapper<Goods> wrapper = Wrappers.query();
        wrapper.select();
        return new Result(true, StatusCode.OK,null,baseMapper.selectList(wrapper));
    }


    @Override
    public Result add(Goods goods) {
        baseMapper.insert(goods);
        return new Result(true,StatusCode.OK,"添加成功",null);
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        IPage<Goods> page1 = new Page<>(page,size);
        QueryWrapper<Goods> wrapper = Wrappers.query();
        wrapper.select();
        IPage<Goods> goodsIPage = baseMapper.selectPage(page1, wrapper);
        PageResult<Goods> pageResult = new PageResult<>();
        pageResult.setRows(goodsIPage.getRecords());
        pageResult.setTotal(goodsIPage.getTotal());
        return new Result(true,StatusCode.OK,"分页查询",pageResult);

    }

    @Override
    public Result findById(Integer id) {
        Goods goods = baseMapper.selectById(id);
        return new Result(true,StatusCode.OK,"根据id查询",goods);
    }

    @Override
    public Result updateByid(Integer id,Goods goods) {
        LambdaUpdateWrapper<Goods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Goods::getId,id);
        baseMapper.update(goods,wrapper);
        return new Result(true,StatusCode.OK,"修改成功",null);
    }

    @Override
    public Result delete(Integer id) {
        baseMapper.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功",null);
    }


}
